#-*- coding=utf-8
import time

# from spider.common.proxy import change_proxy
from BashouScrapy.spiders.wenshu_summary import SummaryHeader

import requests
from PIL import Image, ImageEnhance
from pytesseract import pytesseract

from BashouScrapy.wenshu import common

threshold = 192
table = []
# 二值化 140 192
for index in range(256):
    if index < threshold:
        table.append(0)
    else:
        table.append(1)


def parse_captcha(thread_local_data):
    for i in range(0, 3):
        try:
            # 打开图片
            print ("开始处理普通验证码")
            req = requests.Request("GET", url="http://wenshu.court.gov.cn/User/ValidateCode")
            resp = send_req(req, thread_local_data)
            im = resp.content
            fp = open("temp.jpg", "wb")
            fp.write(im)
            fp.flush()
            fp.close()
            print ("get captcht pic")
            im = Image.open("temp.jpg")
            # 转化到灰度图
            imgry = im.convert('L')
            # 二值化，采用阈值分割法，threshold为分割点
            # 识别
            pytesseract.tesseract_cmd = r'/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6'
            text = pytesseract.image_to_string(imgry)
            print (text)

            check_data = {'ValidateCode': text}
            req = requests.Request("POST", url="http://wenshu.court.gov.cn/Content/CheckVisitCode", data=check_data)
            response = send_req(req, thread_local_data)
            result = response.content
            print (u"验证结果" + result)
            if result == '1':
                return '1'
        except Exception as e:
            print ('处理普通验证码出现错误，异常如下：')
            print (e)
            continue
    return '2'


def send_req(req, thread_local_data):
    session = thread_local_data.session
    prepped = session.prepare_request(req)
    if common.SpiderConf.use_proxy:
        response = session.send(prepped, proxies=thread_local_data.proxy, timeout=30)
    else:
        response = session.send(prepped, timeout=30)
    return response


def parse_new_captcha(header, guid, thread_local_data, redial):
    for i in range(0, 15):
        try:
            print ('开始处理列表验证码，guid:' + guid)
            req = requests.Request("GET", url=("http://wenshu.court.gov.cn/ValiCode/CreateCode/?guid=" + guid))
            res = send_req(req, thread_local_data)
            im = res.content
            if im.find("请开启JavaScript并刷新该页") != -1:
                header["Cookie"] = None
                return 'false', ''
            if im.find("javascript") != -1 or im.lower().find("remind") != -1 or im.find("502 Bad Gateway") != -1:
                print ('验证码请求失败，重播')
                # if SpiderConf.use_proxy:
                #     change_proxy(thread_local_data)
                #     continue
                # else:
                #     redial("redial")
                redial("redial")
            fp = open("temp.png", "wb")
            fp.write(im)
            fp.flush()
            fp.close()
            print ("get captcht pic")
            im = Image.open("temp.png")
            # 转化到灰度图
            imgry = clean_image(im)
            # 识别
            text = pytesseract.image_to_string(imgry, lang="wenshu", config="-psm 7").replace(" ", "")
            print ("result---------------:" + text)
            if len(text) != 4:
                continue

            data = {"number": text, "valiGuid": guid}
            req = requests.Request("POST", data=data, url="http://wenshu.court.gov.cn/List/ValiYzm")
            res = send_req(req, thread_local_data)
            result = res.content
            print (u"验证结果" + result)
            if result == 'true':
                return result, text
            time.sleep(1)
        except Exception as e:
            # change_proxy(thread_local_data)
            print ('处理列表验证码出现错误，异常如下：')
            print (e)
            continue
    return 'false', ''


def download_captcha(get_cookie_by_selenium, get_guid):
    cookie_str = get_cookie_by_selenium(1)
    SummaryHeader.header['cookie'] = cookie_str
    for i in range(0, 500):
        guid = get_guid()
        captcha_url = "http://wenshu.court.gov.cn/ValiCode/CreateCode/?guid=" + guid
        # captcha_url = "http://wenshu.court.gov.cn/waf_captcha/"
        print (str(i) + "url:" + captcha_url)
        # 打开图片
        # captcha_url+=str(int(random.random()*10000))
        res = requests.get(captcha_url, headers=SummaryHeader.header)
        im = res.content
        fp = open("temp.png", "wb")
        fp.write(im)
        fp.flush()
        fp.close()
        print ("get captcht pic")
        im = Image.open("temp.png")
        # 转化到灰度图
        imgry = im.convert('L')
        imgry.save("temp" + str(i) + ".png")


def submit_complex_code(driver):
    for i in range(0, 30):
        try:
            captcha_element = driver.find_element_by_id("Image1")
            driver.execute_script("document.getElementById('Image1').style='width:215px;height:80px'")
            location = captcha_element.location
            x0 = location['x']
            y0 = location['y']

            driver.save_screenshot("captcha_temp.png")  # 截屏，并保存图片
            time.sleep(0.5)
            x1 = x0 + 215
            y1 = y0 + 80
            box = (x0, y0, x1, y1)  # 设置要裁剪的区域
            image = Image.open("captcha_temp.png")
            region = image.crop(box)
            region.save("captcha_crop_temp.png")
            # 获取验证码输入框
            captcha_input = driver.find_element_by_name('captcha')
            # 获取提交按钮
            submit_button = driver.find_element_by_xpath("//input[@type='submit']")

            text = solve_complex_vcode(region)
            if len(text) != 4:
                driver.get("http://wenshu.court.gov.cn/waf_verify.htm")
                continue
            captcha_input.send_keys(text)
            submit_button.click()
            time.sleep(3)
            if driver.page_source.find('网站当前访问量较大，请输入验证码后继续访问') == -1:
                break
        except Exception as e:
            print (e)
            continue


def parse_complex_captcha(thread_local_data):
    for i in range(0, 30):
        captcha_url = "http://wenshu.court.gov.cn/waf_captcha/"
        # 打开图片
        print (captcha_url)
        try:
            session = thread_local_data.session
            if common.SpiderConf.use_proxy:
                print (thread_local_data.proxy_str)
                session.get(url="http://wenshu.court.gov.cn/waf_verify.htm", proxies=thread_local_data.proxy,
                            timeout=20)
                res = session.get(url=captcha_url, proxies=thread_local_data.proxy, timeout=20)
            else:
                session.get(url="http://wenshu.court.gov.cn/waf_verify.htm", timeout=20)
                res = session.get(url=captcha_url, timeout=20)
            im = res.content
            if im.find("javascript") != -1 or im.lower().find("remind") != -1 or im.find("502 Bad Gateway") != -1:
                print ('复杂验证码请求失败，重试')
                if common.SpiderConf.use_proxy:
                    session.get(url="http://wenshu.court.gov.cn/waf_verify.htm", proxies=thread_local_data.proxy,
                                timeout=20)
                else:
                    session.get(url="http://wenshu.court.gov.cn/waf_verify.htm", timeout=20)
                continue
            fp = open("complex_temp.png", "wb")
            fp.write(im)
            fp.flush()
            fp.close()
            print ("get captcht pic")
            im = Image.open("complex_temp.png")
            text = solve_complex_vcode(im)
            print ("result---------------:" + text)
            if len(text) != 4:
                continue

            if common.SpiderConf.use_proxy:
                res = requests.get(url="http://wenshu.court.gov.cn/waf_verify.htm?captcha=" + text,
                                   proxies=thread_local_data.proxy, timeout=20)
            else:
                res = session.get(url="http://wenshu.court.gov.cn/waf_verify.htm?captcha=" + text, timeout=20)
            time.sleep(1)
            result = res.content
        except Exception as e:
            # change_proxy(thread_local_data)
            # if str(es).find("Max retries") != -1:
            print (e)
            continue
        if result.find("首页 - 中国裁判文书网") != -1:
            return result, text
    return 'false', ''


def solve_complex_vcode(im):
    # 去掉噪点
    im = clean_noise(im, im)
    # 彩色模式
    im = im.convert('RGBA')
    # 增大对比度
    enhancer = ImageEnhance.Contrast(im)
    im = enhancer.enhance(1.75)
    # im.show()
    pix_list = im.load()
    p = []
    width, height = im.size
    # 采集干扰线和噪点色彩
    for i in range(width):
        for j in range(0, 15):
            p.append(pix_list[i, j])
        for j in range(65, height):
            p.append(pix_list[i, j])
    for i in range(height):
        for j in range(0, 40):
            p.append(pix_list[j, i])
        for j in range(170, width):
            p.append(pix_list[j, i])
    ps = set(p)
    # print Ps
    # 将采集到的色彩值都置为白色
    for i in range(width):
        for j in range(height):
            if pix_list[i, j] in ps:
                pix_list[i, j] = (255, 255, 255, 255)
    # im.show()
    # 灰度化
    im = im.convert('L')
    # # 二值化，采用阈值分割法，threshold为分割点
    im = im.point(table, '1')
    # im.show()
    # 除去孤立点
    im = clean_noise_point(im)
    # im.show()
    text = pytesseract.image_to_string(im, lang="wenshu", config="-psm 7").replace(' ', '')
    print ("result---------------wenshunew:" + text)
    text = pytesseract.image_to_string(im, lang="eng", config="-psm 7").replace(' ', '')
    print ("result---------------eng:" + text)
    return text


def clean_noise(im, imgry):
    # 灰度化
    im = im.convert('L')
    # # 二值化，采用阈值分割法，threshold为分割点
    im = im.point(table, '1')
    # 除去孤立点
    im = clean_noise_point(im)
    gry_pix_list = imgry.load()
    im_pix_list = im.load()
    width, height = im.size
    for i in range(width):
        for j in range(height):
            if im_pix_list[i, j] == (255, 255, 255, 255):
                gry_pix_list[i, j] = (255, 255, 255, 255)
    # imgry.show()
    return imgry


def collect_new_captcha(guid):
    captcha_url = "http://wenshu.court.gov.cn/ValiCode/CreateCode/?guid=" + guid
    # captcha_url = "http://wenshu.court.gov.cn/waf_captcha/"
    # 打开图片
    # captcha_url+=str(int(random.random()*10000))
    print (captcha_url)
    res = requests.get(captcha_url)
    im = res.content
    fp = open("temp.png", "wb")
    fp.write(im)
    fp.flush()
    fp.close()
    print ("get captcht pic")
    im = Image.open("temp.png")

    imgry = clean_image(im)
    text = pytesseract.image_to_string(imgry, config="-psm 7")
    print ("result---------------:" + text)
    text = pytesseract.image_to_string(imgry, lang="wenshu", config="-psm 7")
    print ("result---------------:" + text)
    text = pytesseract.image_to_string(imgry, lang="yang-0249", config="-psm 7")
    print ("result---------------:" + text)


def clean_noise_point(im):
    data = im.getdata()
    w, h = im.size
    black_point = 0
    survive = 0
    new_img = Image.new("RGBA", im.size, (255, 255, 255))
    for x in range(1, w - 1):
        for y in range(1, h - 1):
            mid_pixel = data[w * y + x]  # 中央像素点像素值
            if mid_pixel == 0:  # 找出上下左右四个方向像素点像素值
                top_pixel = data[w * (y - 1) + x]
                left_pixel = data[w * y + (x - 1)]
                down_pixel = data[w * (y + 1) + x]
                right_pixel = data[w * y + (x + 1)]

                left_top_pixel = data[w * (y - 1) + x - 1]
                right_top_pixel = data[w * (y - 1) + x + 1]
                left_down_pixel = data[w * (y + 1) + x - 1]
                right_down_pixel = data[w * (y + 1) + x + 1]

                # 判断上下左右的黑色像素点总个数
                if top_pixel == 0:
                    black_point += 1
                if left_pixel == 0:
                    black_point += 1
                if down_pixel == 0:
                    black_point += 1
                if right_pixel == 0:
                    black_point += 1
                if left_top_pixel == 0:
                    black_point += 1
                if right_top_pixel == 0:
                    black_point += 1
                if left_down_pixel == 0:
                    black_point += 1
                if right_down_pixel == 0:
                    black_point += 1
                if black_point >= 5:
                    new_img.putpixel((x, y), 0)
                    survive += 1
                # print black_point
                black_point = 0
    print ("去掉了" + str(w * h - survive))
    # new_img.show()

    return new_img


def clean_image(im):
    # # 增大对比度
    # enhancer = ImageEnhance.Contrast(im)
    # im = enhancer.enhance(2)
    # 灰度化
    im = im.convert('L')
    # # 二值化，采用阈值分割法，threshold为分割点
    im = im.point(table, '1')
    # im.show()
    # im = cleanNoisePoint(im)
    # im.show()
    im.save("temp2.png")
    return im


def clean_and_save_images():
    for x in range(0, 499):
        im = Image.open("C:\\Users\\acer\Desktop\\newpics\\temp" + str(x) + ".png")
        im = im.convert('L')
        # 二值化，采用阈值分割法，threshold为分割点
        im = im.point(table, '1')
        text = pytesseract.image_to_string(im, config="-psm 7 wenshu")
        if text != "":
            im.save(r"C:\Users\\acer\\Desktop\\newpicsconvert\\convert" + str(x) + ".png")
